package icedot.library.common.audio;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import java.util.HashMap;

import icedot.library.common.base.Logger;

/**
 * Created by IceDot on 2017/6/16.
 */

public class SoundMgr
{
    private Context _context;
    private SoundPool _soundPool;
    private HashMap<String,Integer> _poolMap;
    private int _maxStream;

    public SoundMgr(Context context,int maxStream)
    {
        _context = context;
        _maxStream = maxStream;
        _soundPool = new SoundPool(_maxStream, AudioManager.STREAM_MUSIC,0);
        _poolMap = new HashMap<>();

        _soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()
        {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
            {
                Logger.showDebugMsg("音乐文件加载完成" + sampleId);
            }
        });
    }

    public boolean addMusicStream(String key,int resId)
    {
        if(_poolMap.size() >= _maxStream)
        {
            Logger.errorMsg("已经超过设置的最大文件数" + _maxStream);
            return false;
        }
        _poolMap.put(key,_soundPool.load(_context,resId,1));
        return true;
    }

    public boolean playSound(String key)
    {
        if(!_poolMap.containsKey(key))
        {
            return false;
        }
        _soundPool.play(_poolMap.get(key),1.0f, 1.0f, 0, 0, 1.0f);
        return true;
    }
}
